

var express = require('express') , ejs = require('ejs') , fs = require('fs') ;
var app = express()  ;
app.use('/public',express.static(__dirname+'/public')) ;

//@note 
var urls = {
	'/index.html' : {
		value : {} ,
		src : '/views/index.html'
	} ,
	'/search/list.html' : {
		value : {} ,
		src : '/views/search/list.html'
	} , 
	'/search/detail.html' :{
		value : {} ,
		src : '/views/search/detail.html'
	}
} ;

var createFunc = function(obj){
	return function(req,res){
		var str = fs.readFileSync(__dirname + obj.src , 'utf8' ) ;
		var ejsStr = ejs.render(str,obj.value) ;
		res.send(ejsStr);
	} ;
} ;

for(var i in urls){	
	app.get(i,createFunc(urls[i])) ;
}

var getListData = require('./service/search.js') ; 
app.get('/search.json',function(req,res){
	var num = req.query.num || 0 ;
	res.send(getListData(parseInt(num)));
}) ;

var server = app.listen(9000,function(){
	console.log( 'Listening on port %d' , server.address().port ) ;
});